package xperiment.metaphor.web.pages;

import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.annotations.Service;
import org.apache.tapestry.beaneditor.Validate;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.annotations.Inject;
import xperiment.metaphor.service.user.UserService;
import xperiment.metaphor.web.MessageKey;

/**
 *
 * @author Mark Ashworth
 */
public class Login {
    @Inject
    private Messages messages;
    
    private String login;
    private String password;
    private int id;

    @Persist
    private boolean error = false;
    
    @Inject
    @Service("userService")
    private UserService userService;

    public Login() {
        System.out.println("login");
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
            
    public String getLogin() {
        return login;
    }

    @Validate("required")
    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    @Validate("required")
    public void setPassword(String password) {
        this.password = password;
    }

    public String getErrorMessage() {
        String ret = null;
        if (error) {
            ret = messages.get(MessageKey.LOGIN_INCORRECT);
        }
        return ret;
    }

    public String onSuccess() {
        
        String ret = "Login";
        error = true;
        
        if (userService.checkLogin(login, password)) {
            error = false;
            ret = "Index";
        }
        return ret;
    }
}
